#include "candidate.h"

#include "ice/candidate.h"

#include <sstream>

namespace xrtc {

/*
priority = (2^24)*(type preference) +
           (2^8)*(local preference) +
           (2^0)*(256 - component ID)
*/
// 0	1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |  NIC Pref     |    Addr Pref  |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// NIC type：3G/wifi
// Addr pref: 定义在RFC3484
uint32_t xrtc::Candidate::get_priority(uint32_t type_preference, 
                                      int network_adapter_preference, 
                                      int replay_preference)
{
    int addr_ref = rtc::IPAddressPrecedence(m_address.ipaddr());
    // local preference =  (NIC Type << 8 | Addr_Pref) + relay preference.
	int local_pref = ((network_adapter_preference << 8) | addr_ref) + replay_preference;
    return (type_preference << 24) | (local_pref << 8) | (256 - (int)m_component);
}

std::string Candidate::to_string() const
{
    std::stringstream ss;
    ss << "Cand[" << m_foundation << ":" << (int)m_component << ":" << m_protocol
       << ":" << m_priority << ":" << m_address.ToString() << ":" << m_type
       << ":" << m_username << ":" << m_password << "]";
    return ss.str();
}

} // namespace xrtc

